import os
import shutil

import pymysql
from xlutils.copy import copy

import pyautogui
import pytesseract
# 导入库
import xlwings as xw
import time
import calendar
import pandas as pd
import xlrd
import xlwt
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import sys
import arrow
from robot import Robot
Robot_d = Robot()


def zz_erp():
    nowdate = '2023-06'
    ipath = 'D:/ExamineVoucher/'
    dpath = os.path.join(os.path.expanduser("~"), 'Downloads/').replace('\\', '/')
    path = 'D:/税务机器人/2023-06/总账数据'
    try:
        print('启动ie前先关闭ie')
        os.system(r"taskkill /F /IM Iexplore.exe")
        print('启动ie')
        Robot_d.ie_ie('http://erp.chinapost.com.cn:8000/')
        time.sleep(5)
        Robot_d.input_name('usernameField', 'yaosheng')
        time.sleep(1)
        Robot_d.input_name('passwordField', 'ys147258')
        time.sleep(1)
        Robot_d.click_id('btn_submit')  # 确定
        time.sleep(10)
        # Robot_d.iframe_select('_pprIFrame')
        time.sleep(3)
        # Robot_d.click_herf("//ul[@id='treemenu1']//a[text()='A41_GL_河南省分公司_总帐查询']")
        Robot_d.click_herf("//ul[@id='treemenu1']//a[substring(text(), string-length(text())-7)='分公司_总帐查询']")
        time.sleep(3)
        Robot_d.click_herf("//ul[@id='treemenu1']//a[text()='查询']")
        time.sleep(1)
        Robot_d.click_herf("//ul[@id='查询账户日记账']//a[text()='账户查询']")
        time.sleep(1)
        Robot_d.click_herf("//li[@id='账户查询']//a[text()='账户查询']")
        time.sleep(20)
        pyautogui.keyDown('Altleft')
        pyautogui.keyDown('v')
        pyautogui.keyUp('v')
        time.sleep(0.5)
        pyautogui.keyDown('r')
        pyautogui.keyUp('r')
        pyautogui.keyUp('Altleft')

        time.sleep(5)
        pyautogui.keyDown('Altleft')
        pyautogui.keyDown('n')
        pyautogui.keyUp('n')
        pyautogui.keyUp('Altleft')
        time.sleep(2)
        pyautogui.press('Enter')
        time.sleep(3)
        Robot.key_txt('CUX:科目按来源展示发生额明细表')
        time.sleep(2)
        pyautogui.press('Tab')
        time.sleep(3)
        pyautogui.press('Tab', 3, 1)
        time.sleep(2)
        Robot.key_txt(nowdate)
        time.sleep(2)
        pyautogui.press('Tab')
        time.sleep(2)
        Robot.key_txt(nowdate)
        time.sleep(2)
        pyautogui.press('Tab', 3, 1)
        time.sleep(2)
        Robot.key_txt('A410001')
        time.sleep(2)
        pyautogui.press('Tab')
        time.sleep(2)
        Robot.key_txt('A410002')
        time.sleep(2)
        pyautogui.press('Enter')
        time.sleep(3)
        pyautogui.keyDown('Altleft')
        pyautogui.keyDown('m')
        pyautogui.keyUp('m')
        pyautogui.keyUp('Altleft')
        time.sleep(3)
        pyautogui.press('Tab')
        time.sleep(3)
        pyautogui.press('Enter')
        time.sleep(3)
        pyautogui.keyDown('Altleft')
        pyautogui.keyDown('i')
        pyautogui.keyUp('i')
        pyautogui.keyUp('Altleft')
        time.sleep(3)
        # asd = 0
        while True:
            # 截图是否更新完，更新完后下载
            time.sleep(30)
            pyautogui.keyDown('Altleft')
            pyautogui.keyDown('r')
            pyautogui.keyUp('r')
            pyautogui.keyUp('Altleft')
            try:
                m, n, o, p = pyautogui.locateOnScreen(ipath + 'image/wancheng.png', confidence=0.8)
                # print(x, y, x + a, y + b)
                im = pyautogui.screenshot(region=(m, n, o, p))
                im.save(ipath + 'image2/wancheng.png')
                c = Robot_d.img_Identify(ipath + 'image/wancheng.png', ipath + 'image2/wancheng.png')
                print(c)
                if c is True:
                    break
                else:
                    pass
            except Exception as e:
                print(e)
                # asd += 1
                # if asd == 20:
                #     raise Exception('again')
                # else:
                #     pass
            # 加结束条件
        time.sleep(3)
        pyautogui.keyDown('Altleft')
        pyautogui.keyDown('p')
        pyautogui.keyUp('p')
        pyautogui.keyUp('Altleft')
        time.sleep(15)
        pyautogui.keyDown('Altleft')
        pyautogui.keyDown('s')
        pyautogui.keyUp('s')
        pyautogui.keyUp('Altleft')
        while True:
            time.sleep(20)
            path_file = dpath
            lists = os.listdir(path_file)
            if len(lists) >= 1:
                lists.sort(key=lambda fn: os.path.getmtime(path_file + fn))
                file_newest = path_file + lists[-1]
                print(file_newest)
                if 'CUX_科目按来源' in file_newest and file_newest[-3:] == 'xls':
                    shutil.move(file_newest, path + '/CUX_科目按来源展示.xls')
                    break
                else:
                    pass
            else:
                continue
        os.system(r"taskkill /F /IM iexplore.exe")

    except Exception as e:
        print(e)
        # os.system(r"taskkill /F /IM iexplore.exe")
